package web.opinion;

import java.sql.SQLException;
import java.util.HashMap;
import com.opensymphony.xwork2.ActionSupport;

import dao.OpinionDao;

public class OpinionService {
	private OpinionDao opinionDao;
	private HashMap<String, Object> paraMap;
	public OpinionService(){
		opinionDao = OpinionDao.getInstance();
		paraMap = new HashMap<String, Object>();
	}

	/**
	 * コンテンツリスト
	 * @param action
	 */
	public void getList(ActionSupport action){
		OpinionAction screenAction = (OpinionAction)action;

		Integer status = screenAction.getPageStatus();
		String opinionid = null;
		if(screenAction.getOpinion() != null)
		opinionid = screenAction.getOpinion().getOpinionid();

		try{
			paraMap.clear();
//			paraMap.put("userid","U0001");
			if(status != null && status >= 0 && status < 5){
				paraMap.put("status", status);
			}
			if(opinionid != null && !opinionid.equals("")){
				paraMap.put("opinionid", opinionid);
			}
			screenAction.setOpinionList(opinionDao.getList(paraMap));
		}catch(SQLException e){
			System.out.println("getList"+e.getMessage());
		}
	}

	/**
	 * コンテンツ検索リスト
	 * @param action
	 */
	public void getSearchList(ActionSupport action){
		OpinionAction screenAction = (OpinionAction)action;

		Integer status = screenAction.getPageStatus();
		String search = screenAction.getSearch();
		String opinionid = screenAction.getOpinionid();
		String readpage = screenAction.getReadpage();
		try{
			paraMap.clear();
			if(opinionid != null && !opinionid.equals("") && readpage.equals("ok")){
				paraMap.put("opinionid", opinionid);
			}
			if(status != null && status >= 0 && status < 5){
				paraMap.put("status", status);
			}
			if(search != null && !search.equals("")){
				paraMap.put("contents", search);
			}
			screenAction.setOpinionList(opinionDao.getSearchList(paraMap));
		}catch(SQLException e){
			System.out.println("getSearchList"+e.getMessage());
		}
	}

	/**
	 * コンテンツ追加
	 * @param action
	 */
	public void regist(ActionSupport action)
	{
		OpinionAction screenAction = (OpinionAction)action;
		String contents = screenAction.getContents();

		try{
			Integer id = opinionDao.selectId();
			paraMap.clear();
			paraMap.put("id", id);
			paraMap.put("contents", contents);
			opinionDao.regist(paraMap);
		}catch(SQLException e){
			System.out.println("regist"+e.getMessage());
		}
	}

	/**
	 * コンテンツにコメント追加
	 * @param action
	 */
	public void registComment(ActionSupport action){
		OpinionAction screenAction = (OpinionAction)action;
		String opinionid = screenAction.getOpinionid();
		String comment = screenAction.getContents();
//		String commentUser = screenAction.getCommentUser();

		try{
			paraMap.clear();
			paraMap.put("opinionid", opinionid);
			paraMap.put("comment", comment);
//			paraMap.put("commentUser", commentUser);
			opinionDao.regist(paraMap);
		}catch(SQLException e){
			System.out.println("registComment"+e.getMessage());
		}
	}


	/**
	 * コンテンツカウント増加
	 * @param action
	 */
	public void addCounter(ActionSupport action){
		OpinionAction screenAction = (OpinionAction)action;
		String opinionid = screenAction.getOpinionid();

		try{
			paraMap.clear();
			paraMap.put("opinionid", opinionid);
			opinionDao.addCounter(paraMap);

			//画面に情報を転送するため、再実行
			this.getSearchList(screenAction);

		}catch(SQLException e){
			System.out.println("addCounter"+e.getMessage());
		}
	}

}
